<?php

namespace App\Http;

use App\Util\Code;
use Carbon\Carbon;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    #regin--------------------   统一处理借口返回错误信息   ----------------------------
    /**
     * 接口统一信息处理
     * @param  Request  $request    错误码及错误信息
     * @param  Request  $data       接口返回成功数据
     * @param  Request  $info       非自定义信息处理
     * @return JsonResponse
     * @throws WorkException
     */
    protected function returnCode( array $responseList, $data = null, $info = '')
    {

        list( $code, $errmsg ) = $responseList;


        $res = ['code' => $code, 'status' => $info ?: $errmsg];

        if ( !is_null( $data ) ) {

            if (is_array($data)) {

                $data = array_filter( $data, function ( $item ) {
                    return $item !== null;
                });

            }

            $res['data'] = $data;

        }

        return response()->json( $res );

    }
    #regin--------------------   统一处理返回错误信息成功入口   ----------------------------
    /**
     * 接口统一信息处理
     * @param  Request  $data       接口返回成功数据
     * @return JsonResponse
     * @throws WorkException
     */
    protected function succ($data = null)
    {
        return $this->returnCode( Code::SUCCESS, $data );
    }

    #regin--------------------   统一处理返回错误信息错误入口   ----------------------------
    /**
     * 接口统一信息处理
     * @param  Request  $responseList   错误列表
     * @return JsonResponse
     * @throws WorkException
     */
    protected function fail(array $responseList = Code::FAIL, $info = '')
    {

        return $this->returnCode($responseList, null, $info);
    }
    #regin--------------------   统一生成图片验证码   ----------------------------
    /**
     * 统一生成图片验证码
     * @param  Request $num  验证码长度
     * @param  Request $width  验证码宽度
     * @param  Request $height  验证码高度
     * @return $data|array()
     */
    public function CreateCaptcha( int $num =4,$width=150,$height=40 ,$font=null)
    {
        $phrase = new PhraseBuilder;
        // 设置验证码位数
        $code = $phrase->build( $num );
        // 生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder($code, $phrase);
        // 设置背景颜色25,25,112
        $builder->setBackgroundColor(25, 25, 112);
        // 设置倾斜角度
        $builder->setMaxAngle(25);
        // 设置验证码后面最大行数
        $builder->setMaxBehindLines(10);
        // 设置验证码前面最大行数
        $builder->setMaxFrontLines(10);
        // 设置验证码颜色
        $builder->setTextColor(254, 67, 101);
        // 可以设置图片宽高及字体
        $builder->build( $width, $height, $font );
        // 获取验证码的内容
        $phrase = $builder->getPhrase();

        // 把内容存入 cache，10分钟后过期
        $client_id = md5( rand(1,1000).time());
        \Cache::put($client_id, $phrase, Carbon::now()->addMinutes(10));

        // 组装接口数据
        $data = [
            'key' => $client_id,
            'captcha' => $builder->inline(),
        ];
        return $data;

    }

}
